package com.xu.system.model.convert;

import com.xu.mybatis.model.PageInfo;
import com.xu.system.model.entity.SysDept;
import com.xu.system.model.pojo.dept.AddDeptReq;
import com.xu.system.model.pojo.dept.DeptSimpleTreeVo;
import com.xu.system.model.pojo.dept.DeptSimpleVo;
import com.xu.system.model.pojo.dept.DeptVo;
import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.List;
import java.util.Map;

/**
 * 部门pojo转换
 *
 * @Author: 许乾伟
 * @Date: 2024/11/4 11:10
 */
@Mapper(componentModel = "spring")
public interface DeptConvert {
    @Mappings({
            @Mapping(target = "createBy", expression = "java(com.xu.security.utils.SecurityUtils.getUserId())"),
            @Mapping(target = "createTime", expression = "java(java.time.LocalDateTime.now())"),
            @Mapping(target = "updateBy", expression = "java(com.xu.security.utils.SecurityUtils.getUserId())"),
            @Mapping(target = "updateTime", expression = "java(java.time.LocalDateTime.now())"),
    })
    SysDept toSysDept(AddDeptReq data);
    @Mappings({
            @Mapping(source = "id", target = "deptId"),
            @Mapping(source = "createTime", target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    })
    DeptVo toDeptVo(SysDept data);

    @Mappings({
            @Mapping(source = "id", target = "deptId"),
            @Mapping(target = "leaderUser",expression = "java(getUserName(data.getLeaderUserId(),userNameMap))"),
            @Mapping(target = "userCount", expression = "java(getUserCount(data.getId(),userCountMap))"),
            @Mapping(target = "statusStr", expression = "java(com.xu.base.enums.StatusEnum.getByStatus(data.getStatus()))"),
            @Mapping(target = "createBy",expression = "java(getUserName(data.getCreateBy(),userNameMap))"),
            @Mapping(target = "createTime",expression = "java(com.xu.base.utils.LocalDateUtils.localDateTimeToyyyymmddhhmmss(data.getCreateTime()))"),
            @Mapping(target = "updateBy",expression = "java(getUserName(data.getUpdateBy(),userNameMap))"),
            @Mapping(target = "updateTime", expression = "java(com.xu.base.utils.LocalDateUtils.localDateTimeToyyyymmddhhmmss(data.getUpdateTime()))")
    })
    DeptSimpleVo toDeptSimpleVo(SysDept data, @Context Map<Long,String> userNameMap, @Context Map<Long,Integer> userCountMap);
    List<DeptSimpleVo> toDeptSimpleVoList(List<SysDept> list, @Context Map<Long,String> userNameMap, @Context Map<Long,Integer> userCountMap);

    PageInfo<DeptSimpleVo> toDeptSimpleVoPage(PageInfo<SysDept> pageData, @Context Map<Long,String> userNameMap, @Context Map<Long,Integer> userCountMap);


    @Mappings({
            @Mapping(source = "id", target = "deptId"),
            @Mapping(target = "leaderUser",expression = "java(getUserName(data.getLeaderUserId(),userNameMap))"),
            @Mapping(target = "userCount", expression = "java(getUserCount(data.getId(),userCountMap))"),
            @Mapping(target = "statusStr", expression = "java(com.xu.base.enums.StatusEnum.getByStatus(data.getStatus()))"),
            @Mapping(target = "createBy",expression = "java(getUserName(data.getCreateBy(),userNameMap))"),
            @Mapping(target = "createTime",expression = "java(com.xu.base.utils.LocalDateUtils.localDateTimeToyyyymmddhhmmss(data.getCreateTime()))"),
            @Mapping(target = "updateBy",expression = "java(getUserName(data.getUpdateBy(),userNameMap))"),
            @Mapping(target = "updateTime", expression = "java(com.xu.base.utils.LocalDateUtils.localDateTimeToyyyymmddhhmmss(data.getUpdateTime()))")
    })
    DeptSimpleTreeVo toDeptSimpleTreeVo(SysDept data, @Context Map<Long,String> userNameMap, @Context Map<Long,Integer> userCountMap);
    List<DeptSimpleTreeVo> toDeptSimpleTreeVoList(List<SysDept> list, @Context Map<Long,String> userNameMap, @Context Map<Long,Integer> userCountMap);

    /**
     * 翻译创建人和更新人
     * @param userId 用户id
     * @param nameMap 用户名称集合
     * @return
     */
    default String getUserName(Long userId, Map<Long,String> nameMap) {
        if (nameMap.containsKey(userId)){
            return nameMap.get(userId);
        }
        return "";
    }
    /**
     * 翻译创建人和更新人
     * @param deptId 部门id
     * @param userCountMap 部门下用户数量
     * @return
     */
    default Integer getUserCount(Long deptId, Map<Long,Integer> userCountMap) {
        if (userCountMap.containsKey(deptId)){
            return userCountMap.get(deptId);
        }
        return 0;
    }

}
